package com.example.administrator.shzs.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.shzs.R;
import com.example.administrator.shzs.parse.ParseOrderList;
import com.example.administrator.shzs.adapter.OrderDetailAdapter;
import com.example.administrator.shzs.url.Contents;
import com.example.administrator.shzs.entity.OrderList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;
//订单详情列表

public class OrderdetailActivity extends Activity implements View.OnClickListener {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.BusinessTitle)
    TextView BusinessTitle;
    @Bind(R.id.Ordertitle)
    RelativeLayout Ordertitle;
    @Bind(R.id.Stroe)
    CircleImageView Stroe;
    @Bind(R.id.turee)
    TextView turee;
    @Bind(R.id.TimeShutDown)
    TextView TimeShutDown;
    @Bind(R.id.order)
    LinearLayout order;
    @Bind(R.id.OrderAddTime)
    TextView OrderAddTime;
    @Bind(R.id.More)
    TextView More;
    @Bind(R.id.BusinessTitleOneImage)
    CircleImageView CivShopImage;
    @Bind(R.id.BusinessTitleOne)
    TextView BusinessTitleOne;
    @Bind(R.id.Go)
    ImageView Go;
    @Bind(R.id.PackMoney)
    TextView PackMoney;
    @Bind(R.id.DeliveryMoney)
    TextView DeliveryMoney;
    @Bind(R.id.SumMoney)
    TextView SumMoney;
    @Bind(R.id.AHSRMoney)
    TextView AHSRMoney;
    @Bind(R.id.ASingeAgain)
    TextView ASingeAgain;
    @Bind(R.id.Time)
    TextView Time;
    @Bind(R.id.address)
    TextView address;
    @Bind(R.id.Horseman)
    CircleImageView Horseman;
    @Bind(R.id.Order)
    TextView Order;
    @Bind(R.id.OrderTime)
    TextView OrderTime;
    @Bind(R.id.remark)
    TextView remark;
    @Bind(R.id.Orderdetail_RecyclerView)
    RecyclerView OrderdetailRecyclerView;
    @Bind(R.id.addname)
    TextView addname;
    @Bind(R.id.addphone)
    TextView addphone;
    @Bind(R.id.HorsemanIphone)
    TextView HorsemanIphone;
    @Bind(R.id.HorsemanName)
    TextView HorsemanName;
    @Bind(R.id.Go_shop)
    RelativeLayout GoShop;
    @Bind(R.id.ll_call_phone)
    LinearLayout llCallPhone;

    private Context context = this;

    //订单号
    private String Oorder;
    //详情订单全局实体类
    private OrderList list;

    //全部订单实体类

    //    订单状态
    private String State = 1 + "";

    //    二级商品
    private OrderDetailAdapter adapter;
    private String urlll; //地址


    //商家图片
    private String shopImage = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderdetail);
//        黄牛刀
        ButterKnife.bind(this);
        //接收数据
        setData();

        setListeners();
        iniDataFromWeb();
    }


    /**
     * 监听
     */
    private void setListeners() {
        More.setOnClickListener(this);
        ASingeAgain.setOnClickListener(this);
        back.setOnClickListener(this);
        GoShop.setOnClickListener(this);
        llCallPhone.setOnClickListener(this);
    }

    /**
     * 接收数据
     */
    private void setData() {

        Intent intent = getIntent();
        Oorder = intent.getStringExtra("orderId");
        State = intent.getStringExtra("State");
        shopImage = intent.getStringExtra("shopImage");

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    int s = Integer.parseInt(State);
                    if (s == 1) {
                        turee.setText("等待审核");
                    } else if (s == 2) {
                        turee.setText("审核通过");
                    } else if (s == 3) {
                        turee.setText("处理成功");
                    } else if (s == 4) {
                        turee.setText("处理失败");
                    } else if (s == 5) {
                        turee.setText("订单取消");
                    } else if (s == 6) {
                        turee.setText("订单失效");
                    } else if (s == 7) {
                        turee.setText("已经调度");
                    } else {
                        turee.setText("请检查您的订单是否有误！");
                    }

                    //拨打配送员电话
                    HorsemanIphone.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent callPhone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + list.getDeliverPhone()));
                            callPhone.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(callPhone);
                        }
                    });


                    Calendar ca = Calendar.getInstance();
                    ca.set(Calendar.MINUTE, ca.get(Calendar.MINUTE) + 20);


                    DisplayImageOptions options = new DisplayImageOptions.Builder()
                            .showImageOnLoading(R.mipmap.user)
                            .showImageForEmptyUri(R.mipmap.user)
                            .showImageOnFail(R.mipmap.user)
                            .cacheInMemory(true)
                            .cacheOnDisk(true)
                            .build();
                    ImageLoader.getInstance().displayImage(shopImage, CivShopImage, options);


                    HorsemanName.setText(list.getDeliverName().equals("") ? "暂无骑手信息" : list.getDeliverName());
                    HorsemanIphone.setText(list.getDeliverPhone());
                    PackMoney.setText(list.getPackagefree());
                    DeliveryMoney.setText(list.getSendmoney());
                    BusinessTitle.setText(list.getTogoName());
                    OrderAddTime.setText(list.getSentTime().substring(11, 16));
                    BusinessTitleOne.setText(list.getTogoName());
                    SumMoney.setText(list.getTotalPrice());
                    AHSRMoney.setText(list.getTotalPrice());
                    Time.setText(list.getOrderTime());
                    address.setText(list.getAddress());
                    Order.setText(list.getOrderID());
                    OrderTime.setText(list.getOrderTime());
                    remark.setText(list.getRemark());
                    addname.setText(list.getUserName());
                    addphone.setText(list.getTel());

                    List<OrderList.FoodlistBean> FoodlistBeans = list.getFoodlist();

                    if (list.getFoodlist().size() > 0) {
                        adapter = new OrderDetailAdapter(context, FoodlistBeans);
                        //布局管理器
                        OrderdetailRecyclerView.setLayoutManager(new LinearLayoutManager(context));
                        OrderdetailRecyclerView.setAdapter(adapter);
                    } else {


                    }
                    break;
            }
        }
    };


    /**
     * 从网络上获取数据
     */
    private void iniDataFromWeb() {

        urlll = Contents.ORDER_LIST + Oorder + "&ordertype=1";
        Request request = new Request.Builder().url(urlll).build();
        Log.i("request", request + "");
        Contents.okhttp.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String request = response.body().string();
                list = ParseOrderList.getOrderList(request);
                if (list != null) {
                    handler.sendEmptyMessage(1);
                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        });
    }

    /**
     * 点击事件
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.More:

                //更多状态
//                Intent intent = new Intent(context, OrderMoreState.class);
//                startActivity(intent);

                break;


            case R.id.ll_call_phone:
                String phone=list.getDeliverPhone().trim();
                if(!phone.equals("") || phone != null) {
                    callPhone(phone);
                }

                break;

            case R.id.ASingeAgain://再来一单
                //跳转到商家
                skipShop();
                break;


            case R.id.back:
                finish();

                break;
            case R.id.Go_shop:

                //跳转到商家
                skipShop();

                break;
            default:           
                break;

        }
    }

    /**
     * 打电话给骑手
     * @param phone 电话
     */
    private void callPhone(String phone) {

        Intent callPhone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
        callPhone.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(callPhone);
    }

    /**
     * 跳转到商家
     */
    private void skipShop() {

        Intent intent = new Intent(this,ShopActivity.class);
        intent.putExtra("isAllOrderFragment",true);
        intent.putExtra("shopId",list.getTogoId());
        intent.putExtra("shopName",list.getTogoName());
        intent.putExtra("shopImage",shopImage);
        intent.putExtra("sendfree",list.getSendmoney());
        startActivity(intent);

    }
}
